package mao.thinkinjava.exception;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Logger;

/**
 * @author Totoro
 * @date 2020/7/21
 * @explain:
 */

class MyException extends Exception{
    Logger logger = Logger.getLogger("MyException");
    MyException(){
        StringWriter writer = new StringWriter();
        printStackTrace(new PrintWriter(writer));
        logger.severe(writer.toString());  //logger 记录会打印在err里面。
    }

    @Override
    public String getMessage() { //相当于普通类的toString方法。
        return "detail message" + super.getMessage();
    }
}
public class TestLogging {
    public static void main(String[] args) {
        try {
            throw  new MyException();
        }catch (MyException e){
            System.out.println(e);
            StackTraceElement[] stackTrace = e.getStackTrace();
            for (StackTraceElement stackTraceElement : stackTrace) {
                System.out.println(stackTraceElement);
            }
            e.initCause(new NullPointerException());
        }
    }
}
